<?php

namespace Wulian\Controller;

use Common\Controller\AdminbaseController;

class AdminPackageController extends AdminbaseController {

    protected $m_WlPackage,$m_card;

    function _initialize() {
        parent::_initialize();
        $this->m_card= D("Wulian/WlCard");
        $this->m_WlPackage= D("Wulian/WlPackage");
    }

    /**
     * 列表页
     */
    public function index(){
        $op = I("get.op");
        if(empty($op)){
            $op = "package_month";
        }

        $where = array("is_del"=>0);
        if($op == 'package_month'){ //月套餐
            $where["package_type"] = 1;
        }else{  //增量包
            $where["package_type"] = 2;
        }

        $count=$this->m_WlPackage->where($where)->count();
        $page = $this->page($count, 15);
        $list = $this->m_WlPackage->where($where)
            ->order("package_status DESC,sort ASC")
            ->limit($page->firstRow , $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("list",$list);
        $this->display(":AdminPackage/".$op);
    }


    /**
     * 新增
     */
    public function add(){
        $is_bind = false;
        $id = I("get.id",0,'intval');
        if(!empty($id)){
            $item=$this->m_WlPackage->where(array('package_id'=>$id))->find();
            $this->assign("item",$item);
            $card = $this->m_card->where(array("package_id_month"=>$id))->find();
            if($card){
                $is_bind = true;
            }
            $this->assign("is_bind",$is_bind);
        }

        $this->display();
    }

    /**
     * 新增数据提交
     */
    public function add_post(){
        if (IS_POST) {
            $id = I("post.id");
            $insert = I("post.");
            $package_data = $insert["package_data"];
            $where = array(
                "package_data"=>$package_data,
                "package_type"=>$insert["package_type"],
                "is_del"=>0,
            );
            if(!empty($id)){
                $where["package_id"] = array("NEQ",$id);
            }
            $aready_package = $this->m_WlPackage->where($where)->find();
            if($aready_package){
                $this->error("该流量对应的套餐已存在！");exit;
            }
            if(round($insert["package_price"],2) != (int)$insert["package_price"]){
                $this->error("价格必须为整数!");exit;
            }
            $insert['package_title_data'] = mToG($insert['package_data']);
            if($insert['package_type'] == 1){   //月套餐
                if(!($package_data>=30 && ((float)($package_data/10) == (int)($package_data/10) || (float)($package_data/1024) == (int)($package_data/1024)))){
                    $this->error("流量值输入有误！");exit;
                }
                $package_title_price = $insert['package_price']."元/月";
            }else{
                if(!((float)($package_data/10) == (int)($package_data/10) || (float)($package_data/1024) == (int)($package_data/1024))){
                    $this->error("流量值输入有误！");exit;
                }
                $package_title_price = $insert['package_price']."元/次";
            }
            $insert['package_title_price'] = $package_title_price;
            if(empty($id)){
                $result =$this->m_WlPackage->add($insert);
                addLog(2,"流量包 新增");
            }else{
                addLog(3,"流量包 修改");
                $card = $this->m_card->where(array("package_id_month"=>$id))->find();
                if(!empty($card)){
                    unset($insert["package_data"]);
                    unset($insert["package_title_data"]);
                }
                $result = $this->m_WlPackage->where(array("package_id"=>$id))->save($insert);
            }
            if ($result) {
                $this->success("操作成功！");
            } else {
                $this->error("操作失败！");
            }

        }
    }
    /**
     * 排序
     */
    public function listorders(){
        $status = parent::_listorders($this->m_WlPackage,'sort');
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

    /**
     * 打开关闭
     */
    public function open_or_close(){
        $id = I("get.id",0,'intval');
        if(empty($id)){
            $this->error("参数获取失败！");exit;
        }
        $active = I('get.active');
        if($active == 'close'){ //关闭
            $result = $this->m_WlPackage->where(array("package_id"=>$id))->save(array("package_status"=>0));
        }elseif($active == 'open'){
            $result = $this->m_WlPackage->where(array("package_id"=>$id))->save(array("package_status"=>1));
        }else{
            $this->error("参数获取失败！");exit;
        }
        if($result){
            addLog(3,"流量包 ".$active);
            $this->success("操作成功！");
        }else{
            $this->success("操作失败！");
        }


    }

    // 删除
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            $card = $this->m_card->where(array("package_id_month"=>$id))->find();
            if(!empty($card)){
                $this->error("该套餐已有卡绑定，无法删除！");
            }else{
                if ($this->m_WlPackage->where(array('package_id'=>$id))->save(array("is_del"=>1)) !==false) {
                    addLog(4,"流量包 删除");
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }

        }else{
            $this->error("参数有误！");
        }

    }

}